// template/other/examine/examineallrole/examineroleindex/examineroleindex.js
const globalIp = getApp().globalData.userIp
Page({

  /**
   * 页面的初始数据
   */
  data: {
    DocEntry: '',
    Role:'',
    CardName: '',
    userName: '',
    canUse: '',
    Authority: '',
    arrayIsEmp: ['生效Y', '失效N'],
    indexIsEmp: 0,
  },

  bindChange1: function(e) {
    this.setData({
      indexIsEmp: e.detail.value
    })
  },

  pageChange: function() {
    wx.navigateTo({
      url: '../examinerolepage/examinerolepage?doc='+this.data.DocEntry
    })
  },

  // 提交
  formSubmit(e) {
    let that = this;
    let roleName = e.detail.value.Role;
    if (roleName == '' || getApp().isNull(roleName)) {
      wx.showModal({
        title: '提示',
        content: '角色名称不能为空!',
        showCancel: false
      })
      return false;
    }
    wx.request({
      url: globalIp + 'home/updateRoleMsg',
      data: {
        openId: wx.getStorageSync('userInfo').openid || '',
        docEntry: that.data.DocEntry,
        value: 2,
        userName: roleName,
        lineStatus: that.data.indexIsEmp == 0 ? 'Y' : 'N'
      },
      header: {
        'content-type': 'application/json'
      },
      method: 'POST',
      success: function(res) {
        if (res.data.success) {
          wx.showToast({
            title: '更新信息成功！',
            icon: 'success',
            success: function(res) {
              setTimeout(() => {
                wx.navigateBack({
                  delta: 1
                });
              }, 1000)
            }
          })
        } else {
          console.log(res.data.msg)
          wx.showToast({
            title: res.data.msg,
            icon: 'none',
            duration: 2000
          })
        }
      },
      fail: function(res) {
        wx.showToast({
          title: res.data.msg,
          icon: 'none',
          duration: 2000
        })
      }
    })
  },

  // 统一请求
  getProject: function(options) {
    let that = this;
    wx.showLoading({
      title: '加载中',
      mask: true
    })
    wx.request({
      url: globalIp + 'home/showAllRole',
      data: {
        docEntry: options.DocEntry || '',
        openId: wx.getStorageSync('userInfo').openid || '',
        value: 2
      },
      header: {
        'content-type': 'application/json'
      },
      method: 'POST',
      success: function(res) {
        if (res.data.success) {
          var object = res.data.JSONObject.data[0];
          that.setData({
            indexIsEmp: object.DocStatus == 'Y' ? 0 : 1,
            UserName: object.UserName,
            CardName: object.CardName,
            DocEntry: object.DocEntry,
            Role: object.Role,
            canUse: object.canUse,
            Authority: object.Authority
          })
        }
      },
      fail: function(res) {
        wx.showToast({
          title: '请返回上一页面，重新请求',
          icon: 'none',
          duration: 2000,
        })
      },
      complete: function(res) {
        wx.hideLoading()
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getProject(options);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

})